package Test;

public class Test1 {

//    private static class MyRunnable implements Runnable {
//        public volatile boolean isQuit = false;
//        @Override
//        public void run() {
//            while (!isQuit) {
//                System.out.println(Thread.currentThread().getName() + ": 正在交易中");
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//            System.out.println(Thread.currentThread().getName() + "有内鬼，终止交易！");
//        }
//    }
//
//    public static void main(String[] args) throws InterruptedException {
//        MyRunnable target = new MyRunnable();
//        Thread thread = new Thread(target, "陈永仁");
//        System.out.println(Thread.currentThread().getName() + ": 让陈永仁开始转账。");
//        thread.start();
//        Thread.sleep(1000);
//        System.out.println(Thread.currentThread().getName() + ": 刘建明来电话了，得赶紧通知陈永仁对方是卧底！");
//        target.isQuit = true;
//    }


    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            // 两种⽅法均可以
            while (!Thread.interrupted()) {
                //while (!Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName() + ": 正在交易中");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(Thread.currentThread().getName() + ": 有内⻤，终⽌交易！");
                    // 注意此处的 break
                    break;
                }
            }
            System.out.println(Thread.currentThread().getName() + ": 有内⻤，终⽌交易！");
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyRunnable target = new MyRunnable();
        Thread thread = new Thread(target, "陈永仁");
        System.out.println(Thread.currentThread().getName() + ": 让陈永仁开始转账");
        thread.start();
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + ": 刘建明来电话了，得赶紧通知陈永仁对方是卧底！");
        thread.interrupt();
    }


}
